@use 'sass:string';
@use 'sass:math';

/**
 * WMS样式混合器
 * 提供常用的样式混合器和工具函数
 */

// 响应式断点混合器
@mixin wms-respond-to($breakpoint) {
  @if $breakpoint == xs {
    @media (max-width: 479px) { @content; }
  }
  @if $breakpoint == sm {
    @media (min-width: 480px) and (max-width: 575px) { @content; }
  }
  @if $breakpoint == md {
    @media (min-width: 576px) and (max-width: 767px) { @content; }
  }
  @if $breakpoint == lg {
    @media (min-width: 768px) and (max-width: 991px) { @content; }
  }
  @if $breakpoint == xl {
    @media (min-width: 992px) and (max-width: 1199px) { @content; }
  }
  @if $breakpoint == xxl {
    @media (min-width: 1200px) { @content; }
  }
}

// 移动端优先响应式
@mixin wms-mobile-first($breakpoint) {
  @if $breakpoint == sm {
    @media (min-width: 480px) { @content; }
  }
  @if $breakpoint == md {
    @media (min-width: 576px) { @content; }
  }
  @if $breakpoint == lg {
    @media (min-width: 768px) { @content; }
  }
  @if $breakpoint == xl {
    @media (min-width: 992px) { @content; }
  }
  @if $breakpoint == xxl {
    @media (min-width: 1200px) { @content; }
  }
}

// 桌面端优先响应式
@mixin wms-desktop-first($breakpoint) {
  @if $breakpoint == xl {
    @media (max-width: 1199px) { @content; }
  }
  @if $breakpoint == lg {
    @media (max-width: 991px) { @content; }
  }
  @if $breakpoint == md {
    @media (max-width: 767px) { @content; }
  }
  @if $breakpoint == sm {
    @media (max-width: 575px) { @content; }
  }
  @if $breakpoint == xs {
    @media (max-width: 479px) { @content; }
  }
}

// 清除浮动
@mixin wms-clearfix {
  &::after {
    content: '';
    display: table;
    clear: both;
  }
}

// 文本省略
@mixin wms-text-ellipsis($lines: 1) {
  @if $lines == 1 {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  } @else {
    display: -webkit-box;
    -webkit-line-clamp: $lines;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

// 居中对齐
@mixin wms-center($type: both) {
  position: absolute;
  
  @if $type == both {
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
  @if $type == horizontal {
    left: 50%;
    transform: translateX(-50%);
  }
  @if $type == vertical {
    top: 50%;
    transform: translateY(-50%);
  }
}

// Flex布局
@mixin wms-flex($direction: row, $justify: flex-start, $align: stretch, $wrap: nowrap) {
  display: flex;
  flex-direction: $direction;
  justify-content: $justify;
  align-items: $align;
  flex-wrap: $wrap;
}

// Flex居中
@mixin wms-flex-center {
  @include wms-flex(row, center, center);
}

// Grid布局
@mixin wms-grid($columns: 1fr, $rows: auto, $gap: 0) {
  display: grid;
  grid-template-columns: $columns;
  grid-template-rows: $rows;
  gap: $gap;
}

// 按钮样式
@mixin wms-button($bg-color: var(--wms-color-primary), $text-color: #fff, $border-color: transparent) {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: var(--wms-button-padding-md);
  border: 1px solid $border-color;
  border-radius: var(--wms-border-radius-md);
  background-color: $bg-color;
  color: $text-color;
  font-size: var(--wms-font-size-md);
  font-weight: var(--wms-font-weight-medium);
  line-height: 1;
  cursor: pointer;
  transition: var(--wms-transition-all);
  user-select: none;
  
  &:hover {
    opacity: 0.8;
  }
  
  &:active {
    transform: translateY(1px);
  }
  
  &:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    
    &:hover,
    &:active {
      opacity: 0.5;
      transform: none;
    }
  }
}

// 输入框样式
@mixin wms-input {
  width: 100%;
  padding: var(--wms-input-padding);
  border: 1px solid var(--wms-border-color-base);
  border-radius: var(--wms-border-radius-md);
  background-color: var(--wms-fill-color-blank);
  color: var(--wms-color-text-primary);
  font-size: var(--wms-font-size-md);
  line-height: 1.5;
  transition: var(--wms-transition-all);
  
  &:focus {
    border-color: var(--wms-color-primary);
    outline: none;
    box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
  }
  
  &:disabled {
    background-color: var(--wms-fill-color-light);
    color: var(--wms-color-text-disabled);
    cursor: not-allowed;
  }
  
  &::placeholder {
    color: var(--wms-color-text-placeholder);
  }
}

// 卡片样式
@mixin wms-card($padding: var(--wms-card-padding)) {
  background: var(--wms-fill-color-blank);
  border: 1px solid var(--wms-border-color-light);
  border-radius: var(--wms-border-radius-lg);
  box-shadow: var(--wms-box-shadow-light);
  padding: $padding;
  transition: var(--wms-transition-all);
  
  &:hover {
    box-shadow: var(--wms-box-shadow-base);
  }
}

// 状态标签样式
@mixin wms-status-tag($bg-color, $text-color: #fff, $border-color: transparent) {
  display: inline-flex;
  align-items: center;
  padding: 4px 8px;
  border: 1px solid $border-color;
  border-radius: var(--wms-border-radius-sm);
  background-color: $bg-color;
  color: $text-color;
  font-size: var(--wms-font-size-xs);
  font-weight: var(--wms-font-weight-medium);
  line-height: 1;
}

// 阴影效果
@mixin wms-shadow($level: 1) {
  @if $level == 1 {
    box-shadow: var(--wms-box-shadow-light);
  }
  @if $level == 2 {
    box-shadow: var(--wms-box-shadow-base);
  }
  @if $level == 3 {
    box-shadow: var(--wms-box-shadow-dark);
  }
}

// 过渡动画
@mixin wms-transition($property: all, $duration: var(--wms-transition-duration-base), $timing: var(--wms-transition-ease-in-out)) {
  transition: $property $duration $timing;
}

// 渐变背景
@mixin wms-gradient($start-color, $end-color, $direction: to right) {
  background: linear-gradient($direction, $start-color, $end-color);
}

// 滚动条样式
@mixin wms-scrollbar($width: 6px, $track-color: var(--wms-fill-color-light), $thumb-color: var(--wms-border-color-base)) {
  &::-webkit-scrollbar {
    width: $width;
    height: $width;
  }
  
  &::-webkit-scrollbar-track {
    background: $track-color;
    border-radius: math.div($width, 2);
  }
  
  &::-webkit-scrollbar-thumb {
    background: $thumb-color;
    border-radius: math.div($width, 2);
    
    &:hover {
      background: var(--wms-border-color-darker);
    }
  }
}

// 加载动画
@mixin wms-loading-spin {
  animation: wms-spin 1s linear infinite;
}

@keyframes wms-spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

// 淡入动画
@mixin wms-fade-in($duration: var(--wms-transition-duration-base)) {
  animation: wms-fade-in $duration ease-in-out;
}

@keyframes wms-fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

// 滑入动画
@mixin wms-slide-in($direction: up, $distance: 20px, $duration: var(--wms-transition-duration-base)) {
  animation: wms-slide-in-#{$direction} $duration ease-out;
}

@keyframes wms-slide-in-up {
  from {
    transform: translateY(20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

@keyframes wms-slide-in-down {
  from {
    transform: translateY(-20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

@keyframes wms-slide-in-left {
  from {
    transform: translateX(-20px);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

@keyframes wms-slide-in-right {
  from {
    transform: translateX(20px);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

// 工具类生成器
@mixin wms-generate-spacing-utilities {
  @each $prop in (margin, padding) {
    @each $size, $value in (xs: 4px, sm: 8px, md: 12px, lg: 16px, xl: 20px, xxl: 24px) {
      .wms-#{string.slice($prop, 1, 1)}-#{$size} { #{$prop}: $value !important; }
      .wms-#{string.slice($prop, 1, 1)}t-#{$size} { #{$prop}-top: $value !important; }
      .wms-#{string.slice($prop, 1, 1)}r-#{$size} { #{$prop}-right: $value !important; }
      .wms-#{string.slice($prop, 1, 1)}b-#{$size} { #{$prop}-bottom: $value !important; }
      .wms-#{string.slice($prop, 1, 1)}l-#{$size} { #{$prop}-left: $value !important; }
      .wms-#{string.slice($prop, 1, 1)}x-#{$size} { 
        #{$prop}-left: $value !important;
        #{$prop}-right: $value !important;
      }
      .wms-#{string.slice($prop, 1, 1)}y-#{$size} { 
        #{$prop}-top: $value !important;
        #{$prop}-bottom: $value !important;
      }
    }
  }
}

// 生成间距工具类
@include wms-generate-spacing-utilities;
